using System.IO;
using System.Text;
using Framework.Core.Io;

namespace Framework.Core.Process.Tasks
{
    internal struct PsExec : ICommand
    {
        private const string PsExecPath = @"PsExec.exe";
        private static readonly string PsExecName = Path.GetFileNameWithoutExtension(PsExecPath);

        public string Name { get { return PsExecName; } }
        public string FileName { get { return PsExecPath; } }
        public string Arguments { get; private set; }
        public bool IsNative { get { return false; } }

        static PsExec()
        {
            PsExecPath.Deploy();
        }

        public PsExec(string remoteMachine, string remoteUser, string remotePassword, ICommand command)
            : this()
        {
            var args = new StringBuilder("-accepteula ");

            if (!command.IsNative)
            {
                args.Append("-c -v ");
            }

            if (!string.IsNullOrWhiteSpace(remoteUser))
            {
                args.AppendFormat(@"-u ""{0}"" -p ""{1}"" ", remoteUser, remotePassword);
            }

            args.AppendFormat(@"""\\{0}"" ""{1}"" {2}", remoteMachine, command.FileName, command.Arguments);

            Arguments = args.ToString();
        }
    }
}
